{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 量子测量\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_quantum_measurement.ipynb)&emsp;\n",
    "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_quantum_measurement.py)&emsp;\n",
    "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/docs/mindquantum/docs/source_zh_cn/quantum_measurement.ipynb)\n",
    "\n",
    "## 概述\n",
    "\n",
    "在量子线路设计时，我们最终需要通过测量(measure)操作获得结果，进行测量的时候需要选定特定的基态进行测量，而测量得到的结果是不确定的，测量后量子态也会随机坍塌到我们测量的某个基态上。\n",
    "\n",
    "量子测量由一组测量算子${M_m}$描述，这些算子作用在被测系统状态空间上，指标$m$表示实验中可能的测量结果，若在测量前，量子系统的状态为$|\\psi⟩$，则结果$m$发生的可能性为：\n",
    "\n",
    "$$\n",
    "p(m)=⟨\\psi|M^\\dagger_mM_m|\\psi⟩\n",
    "$$\n",
    "\n",
    "测量后系统的状态塌缩为：\n",
    "\n",
    "$$\n",
    "\\frac{M_m|\\psi⟩}{\\sqrt{⟨\\psi|M^\\dagger_mM_m|\\psi⟩}}\n",
    "$$\n",
    "\n",
    "测量算子满足完备性方程：\n",
    "\n",
    "$$\n",
    "\\Sigma_mM^\\dagger_mM_m=I\n",
    "$$\n",
    "\n",
    "完备性方程表达了概率之和为1的事实：\n",
    "\n",
    "$$\n",
    "1=\\Sigma_m p(m)=\\Sigma_m ⟨\\psi|M^\\dagger_mM_m|\\psi⟩\n",
    "$$\n",
    "\n",
    "该方程对所有的$|\\psi⟩$都成立，与完备性方程等价，但直接验证完备性方程更简单，所以将完备性方程作为约束条件。\n",
    "\n",
    "根据选取测量算子的不同，我们常见的测量分成计算基测量、投影测量、Pauli测量等，MindQuantum提供了丰富的测量功能与可视化展示工具，我们利用这些功能进一步学习量子测量。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算基测量\n",
    "\n",
    "我们先对计算基测量有一个简单认识：假设有一个n个量子比特的态，我们对它执行n比特计算基测量，测量后，如果结果为$00 \\cdots0$，表明该n量子比特系统的量子状态已塌缩到$|00 \\cdots0⟩$态；类似地，如果测量其中一个量子比特，那么它表示的$2^n$种情况就会被排除掉一半，即在两个各占一半的空间中，测量操作将量子态投影到其中一个空间，表明该n量子比特系统的量子状态中一个子系统塌缩了。\n",
    "\n",
    "### 单量子比特在计算基下的测量\n",
    "\n",
    "计算基测量算子：$M_0=|0⟩⟨0|$和$M_1=|1⟩⟨1|$，注意到每个测量算子都是Hermite的，即满足$M_0^\\dagger=M_0,M_1^\\dagger=M_1$，并且$M^2_0=M_0,M^2_1=M_1$，于是满足完备性关系：\n",
    "\n",
    "$$\n",
    "I=M^\\dagger_0M_0+M^\\dagger_1M_1=M_0+M_1\n",
    "$$\n",
    "\n",
    "假设被测量状态$|\\psi⟩=a|0⟩+b|1⟩$，则获得测量结果0的概率是：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(0)&=⟨\\psi|M^\\dagger_0M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|(|0⟩⟨0|)|\\psi⟩\\\\\n",
    "&=(⟨\\psi|0⟩)(⟨0|\\psi⟩)\\\\\n",
    "&=[(⟨0|a^{\\star}+⟨1|b^{\\star})|0⟩][⟨0|(a|0⟩+b|1⟩)]\\\\\n",
    "&=(a^{\\star}⟨0|0⟩+b^{\\star}⟨1|0⟩)(a⟨0|0⟩+b⟨1|0⟩)\\\\\n",
    "&=a^{\\star}a\\\\\n",
    "&=|a|^2\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "类似地，获得测量结果1的概率是$p(1)=|b|^2$。两种情况下，测量后的状态分别为：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{M_0|\\psi⟩}{|a|}=\\frac{a}{|a|}|0⟩\\\\\n",
    "\\frac{M_1|\\psi⟩}{|b|}=\\frac{b}{|b|}|1⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "### 多量子比特在计算基下的测量——以双量子比特为例\n",
    "\n",
    "#### 测量系统中所有比特\n",
    "\n",
    "双量子比特系统下计算基测量算子：$M_{00}=|00⟩⟨00|,M_{01}=|01⟩⟨01|,M_{10}=|10⟩⟨10|$和$M_{11}=|11⟩⟨11|$，注意到每个测量算子都是Hermite的，即满足$M_{ij}^\\dagger=M_{ij},i,j\\in\\{0,1\\}$，并且$M_{ij}^2=M_{ij}$，于是满足完备性关系：\n",
    "\n",
    "$$\n",
    "I=M^\\dagger_{00}M_{00}+M^\\dagger_{01}M_{01}+M^\\dagger_{10}M_{10}+M^\\dagger_{11}M_{11}=M_{00}+M_{01}+M_{10}+M_{11}\n",
    "$$\n",
    "\n",
    "假设被测量状态$|\\psi⟩=a|00⟩+b|01⟩+c|10⟩+d|11⟩$，则获得测量结果00的概率是：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(00)&=⟨\\psi|M^\\dagger_{00}M_{00}|\\psi⟩\\\\\n",
    "&=⟨\\psi|M_{00}|\\psi⟩\\\\\n",
    "&=⟨\\psi|(|00⟩⟨00|)|\\psi⟩\\\\\n",
    "&=(⟨\\psi|00⟩)(⟨00|\\psi⟩)\\\\\n",
    "&=[(⟨00|a^{\\star}+⟨01|b^{\\star}+⟨10|c^{\\star}+⟨11|d^{\\star})|00⟩][⟨00|(a|00⟩+b|01⟩+c|10⟩+d|11⟩)]\\\\\n",
    "&=(a^{\\star}⟨00|00⟩+b^{\\star}⟨01|00⟩+c^{\\star}⟨10|00⟩+d^{\\star}⟨11|00⟩)(a⟨00|00⟩+b⟨00|01⟩+c⟨00|10⟩+b⟨00|11⟩)\\\\\n",
    "&=a^{\\star}a\\\\\n",
    "&=|a|^2\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "类似地，获得测量结果01的概率是$p(01)=|b|^2$，10的概率是$p(10)=|c|^2$，11的概率是$p(11)=|d|^2$。四种情况下，测量后的状态分别为：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{M_{00}|\\psi⟩}{|a|}=\\frac{a}{|a|}|00⟩\\\\\n",
    "\\frac{M_{01}|\\psi⟩}{|b|}=\\frac{b}{|b|}|01⟩\\\\\n",
    "\\frac{M_{10}|\\psi⟩}{|c|}=\\frac{c}{|c|}|10⟩\\\\\n",
    "\\frac{M_{11}|\\psi⟩}{|d|}=\\frac{d}{|d|}|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "#### 测量系统中单个比特\n",
    "\n",
    "如果测量双量子比特量子状态的第一个量子比特，双计算基测量算子：$M_0=|0⟩⟨0|\\otimes I$和$M_1=|1⟩⟨1|\\otimes I$，注意到每个测量算子都是Hermite的，即满足$M_0^\\dagger=M_0,M_1^\\dagger=M_1$，并且$M^2_0=M_0,M^2_1=M_1$，于是满足完备性关系：\n",
    "\n",
    "$$\n",
    "I=M^\\dagger_0M_0+M^\\dagger_1M_1=M_0+M_1\n",
    "$$\n",
    "\n",
    "假设被测量状态$|\\psi⟩=a|00⟩+b|01⟩+c|10⟩+d|11⟩$，则测量双量子比特量子状态的第一个量子比特，获得测量结果0的概率是：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(0)&=⟨\\psi|M^\\dagger_0M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|M_0|\\psi⟩\\\\\n",
    "&=⟨\\psi|(|0⟩⟨0|\\otimes I)|\\psi⟩\\\\\n",
    "&=(⟨00|a^{\\star}+⟨01|b^{\\star}+⟨10|c^{\\star}+⟨11|d^{\\star})|(|0⟩⟨0|\\otimes I)|(a|00⟩+b|01⟩+c|10⟩+d|11⟩)\\\\\n",
    "&=(⟨00|a^{\\star}+⟨01|b^{\\star}+⟨10|c^{\\star}+⟨11|d^{\\star})|(a|00⟩+b|01⟩)\\\\\n",
    "&=a^{\\star}a+b^{\\star}b\\\\\n",
    "&=|a|^2+|b|^2\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "类似地，获得测量结果1的概率是$p(1)=|c|^2+|d|^2$。两种情况下，测量后的状态分别为：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{M_0|\\psi⟩}{\\sqrt{|a|^2+|b|^2}}=\\frac{a}{\\sqrt{|a|^2+|b|^2}}|00⟩+\\frac{b}{\\sqrt{|a|^2+|b|^2}}|01⟩\\\\\n",
    "\\frac{M_1|\\psi⟩}{\\sqrt{|c|^2+|d|^2}}=\\frac{c}{\\sqrt{|c|^2+|d|^2}}|10⟩+\\frac{d}{\\sqrt{|c|^2+|d|^2}}|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "通过对计算基测量的学习，我们可以直观认识到，在多量子比特态的其中一个比特上做测量，本质是将量子态投影到两个子空间之一中。为了简洁的区分出这两个子空间，我们利用线性代数知识知道，可以通过恰好有两个唯一特征值的矩阵来描述两个正交子空间。\n",
    "\n",
    "### 计算基测量的MindQuantum实现\n",
    "\n",
    "接下来我们使用MindQuantum搭建一个含测量操作的量子线路并观察结果，首先导入本教程所依赖的模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np                           # 导入numpy库并简写为np\n",
    "from mindquantum.core import X, H            # 导入量子门H, X\n",
    "from mindquantum.simulator import Simulator  # 从mindquantum.simulator中导入Simulator类\n",
    "from mindquantum.core import Circuit         # 导入Circuit模块，用于搭建量子线路\n",
    "from mindquantum import Measure              # 引入测量门"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "说明：\n",
    "\n",
    "（1）numpy是一个功能强大的Python库，主要用于对多维数组执行计算，支持大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库；\n",
    "\n",
    "（2）mindquantum是量子-经典混合计算框架，支持多种量子神经网络的训练和推理；\n",
    "\n",
    "（3）搭建的量子线路中所需执行的量子门需要从mindquantum.core模块中导入；\n",
    "\n",
    "（4）运行量子线路所需要的量子模拟器需要从mindquantum.simulator模块中导入；\n",
    "\n",
    "（5）搭建量子线路所需要的量子线路类Circuit需要从mindquantum.core模块中导入；\n",
    "\n",
    "（6）对量子线路进行测量需要从mindquantum中导入Measure操作。\n",
    "\n",
    "我们搭建出一个制备双量子比特均匀叠加态$|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$的量子线路，并分别展示在所有量子比特上使用计算基测量和只在0号量子比特上使用计算基测量的结果。\n",
    "\n",
    "#### MindQuantum实现测量系统中所有比特\n",
    "\n",
    "在使用代码演示之前，我们先简单计算出理论值。\n",
    "\n",
    "在所有量子比特上使用计算基测量$|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(00)&=|a|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "p(01)&=|b|^2=0^2=0\\\\\n",
    "p(10)&=|c|^2=0^2=0\\\\\n",
    "p(11)&=|d|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "可以看到，测量结果只有两种可能：00和11，概率均是$\\frac{1}{2}$。测量后的状态分别为：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{a}{|a|}|00⟩=|00⟩\\\\\n",
    "\\frac{d}{|d|}|11⟩=|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "我们开始搭建制备$|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$并在所有比特上做测量的量子线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"276.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 110.4 A 12.8 12.8 0 0 1 225.60000000000002 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 93.93539030917347 L 225.21569219381655 89.13539030917347 L 225.21569219381655 98.73539030917347 L 221.8901546432843 96.81539030917347 L 214.04707658144957 110.4 L 212.38430780618347 109.44 L 220.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f162cae3e20>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ_all = Circuit()             # 初始化量子线路\n",
    "circ_all += H.on(0)              # H门作用在第0位量子比特\n",
    "circ_all += X.on(1, 0)           # X门作用在第1位量子比特且受第0位量子比特控制\n",
    "circ_all += Measure('q0').on(0)  # 在0号量子比特作用一个测量，并将该测量命名为'q0'\n",
    "circ_all += Measure('q1').on(1)  # 在1号量子比特作用一个测量，并将该测量命名为'q1'\n",
    "circ_all.svg()                   # 绘制SVG格式的量子线路图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"117.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.2\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.4\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.6\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.8\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n1.0\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n11\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988508798485442\" fill=\"#5e7ce0\" />\n<text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988508798514190\" fill-opacity=\"0\" >\n1\n </text>\n<animate xlink:href=\"#bar_0_1649988508798485442\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988508798485442\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988508798514190\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f162ca6ea00>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim = Simulator('projectq', 2)  # 声明一个2比特的projectq模拟器\n",
    "sim.apply_circuit(circ_all).svg()     # 在模拟器上运行量子线路"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到我们得到的测量结果是'00'，测量后的量子态塌缩为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦11⟩\n"
     ]
    }
   ],
   "source": [
    "print(sim.get_qs(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "量子态塌缩成了$1|00⟩$，与理论值相符。\n",
    "\n",
    "如果我们多测量几次，可以发现测量结果也会为'11'："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"117.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.2\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.4\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.6\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.8\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n1.0\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n00\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988508969500642\" fill=\"#5e7ce0\" />\n<text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988508969531308\" fill-opacity=\"0\" >\n1\n </text>\n<animate xlink:href=\"#bar_0_1649988508969500642\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988508969500642\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988508969531308\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f15ec8254f0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()  #复位模拟器\n",
    "sim.apply_circuit(circ_all).svg()  # 在模拟器上运行量子线路"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "打印出此时量子态，可以看到它坍缩成了相应的$|11⟩$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦00⟩\n"
     ]
    }
   ],
   "source": [
    "print(sim.get_qs(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们观察到，测量结果时而为'00'时而为'11'，符合理论预期，但是没有办法观察出现00和11的概率是否相同，我们希望可以多次测量，统计出不同结果出现的频率，以此观察结果是否满足预期的概率分布。为此我们使用量子线路采样(Sampling)功能："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"408.4\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0 q1\n </text>\n<line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.101\n </text>\n<line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.202\n </text>\n<line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.303\n </text>\n<line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.404\n </text>\n<line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.505\n </text>\n<line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n00\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"73.0\" width=\"294.05940594059405\" height=\"24\" id=\"bar_0_1649988509242993340\" fill=\"#5e7ce0\" />\n<text x=\"342.459405940594\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988509243015428\" fill-opacity=\"0\" >\n495\n </text>\n<text x=\"29.4\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n11\n </text>\n<line x1=\"31.4\" x2=\"38.4\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"38.4\" y=\"103.0\" width=\"300.0\" height=\"24\" id=\"bar_1_1649988509243035485\" fill=\"#16acff\" />\n<text x=\"348.4\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1649988509243054811\" fill-opacity=\"0\" >\n505\n </text>\n<animate xlink:href=\"#bar_0_1649988509242993340\" attributeName=\"width\" from=\"0\" to=\"294.05940594059405\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 294.05940594059405\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509243035485\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509243035485\" attributeName=\"fill\" from=\"#16acff\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#16acff; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988509243015428\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1649988509243054811\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f15caaffdc0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()\n",
    "result = sim.sampling(circ_all, shots=1000)  # 对上面定义的线路采样1000次\n",
    "result.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以看到，采样1000中，'00'出现了499次，'11'出现了501次，采样结果符合概率分布，细微的误差是由模拟器噪声导致。仔细阅读的同学可以发现，在[量子模拟器教程](https://gitee.com/buyulin/mindquantum/blob/master/tutorials/quantum_simulator.ipynb)中我们已经展示过该线路的采样结果，但并未解释结果如是分布的原因，在本教程中学习了计算基测量后，相信同学们对该结果分布的认识更加深刻。\n",
    "\n",
    "#### MindQuantum实现测量系统中单个比特\n",
    "\n",
    "同样地，在使用代码演示之前，我们先简单计算出理论值。\n",
    "\n",
    "在0号量子比特上使用计算基测量$|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "p(0)=|a|^2+|b|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "p(1)=|c|^2+|d|^2=(\\frac{\\sqrt{2}}{{2}})^2=\\frac{1}{2}\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "可以看到，测量结果有两种可能：0和1，概率均是$\\frac{1}{2}$。测量后的状态分别为：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\frac{a}{\\sqrt{|a|^2+|b|^2}}|00⟩+\\frac{b}{\\sqrt{|a|^2+|b|^2}}|01⟩=|00⟩\\\\\n",
    "\\frac{c}{\\sqrt{|c|^2+|d|^2}}|10⟩+\\frac{d}{\\sqrt{|c|^2+|d|^2}}|11⟩=|11⟩\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "我们开始搭建制备$|\\psi⟩=\\frac{\\sqrt{2}(|00⟩+|11⟩)}{2}$并在0号量子比特上做测量的量子线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"276.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f15c328d310>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ_partial = Circuit()             # 初始化量子线路\n",
    "circ_partial += H.on(0)              # H门作用在第0位量子比特\n",
    "circ_partial += X.on(1, 0)           # X门作用在第1位量子比特且受第0位量子比特控制\n",
    "circ_partial += Measure('q0').on(0)  # 在0号量子比特作用一个测量，并将该测量命名为'q0'\n",
    "circ_partial.svg()                   # 绘制SVG格式的量子线路图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"117.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0\n </text>\n<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.2\n </text>\n<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.4\n </text>\n<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.6\n </text>\n<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.8\n </text>\n<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n1.0\n </text>\n<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n0\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1649988509362739310\" fill=\"#5e7ce0\" />\n<text x=\"341.2\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988509362766672\" fill-opacity=\"0\" >\n1\n </text>\n<animate xlink:href=\"#bar_0_1649988509362739310\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_0_1649988509362739310\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988509362766672\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f162ca8fe80>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()                            # 复位模拟器\n",
    "sim.apply_circuit(circ_partial).svg()  # 在模拟器上运行量子线路"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到我们得到的测量结果是'0'，测量后的量子态塌缩为："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦00⟩\n"
     ]
    }
   ],
   "source": [
    "print(sim.get_qs(True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "量子态塌缩成了$1|00⟩$，与理论值相符。\n",
    "\n",
    "同样地，如果我们多测量几次，可以发现测量结果也会为'1'，此处不再演示。我们直接对该量子线路采样1000次观察结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0\n </text>\n<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.102\n </text>\n<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.205\n </text>\n<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.307\n </text>\n<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.41\n </text>\n<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.512\n </text>\n<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n0\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"73.0\" width=\"285.9375\" height=\"24\" id=\"bar_0_1649988509781221380\" fill=\"#5e7ce0\" />\n<text x=\"327.1375\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1649988509781242592\" fill-opacity=\"0\" >\n488\n </text>\n<text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n1\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"103.0\" width=\"300.0\" height=\"24\" id=\"bar_1_1649988509781262122\" fill=\"#16acff\" />\n<text x=\"341.2\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1649988509781279156\" fill-opacity=\"0\" >\n512\n </text>\n<animate xlink:href=\"#bar_0_1649988509781221380\" attributeName=\"width\" from=\"0\" to=\"285.9375\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 285.9375\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509781262122\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1649988509781262122\" attributeName=\"fill\" from=\"#16acff\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#16acff; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1649988509781242592\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1649988509781279156\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f15cab14fa0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.reset()\n",
    "result = sim.sampling(circ_partial, shots=1000)  # 对上面定义的线路采样1000次\n",
    "result.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以看到，采样1000中，'0'出现了499次，'1'出现了501次。采样结果符合概率分布，细微的误差是由模拟器噪声导致。\n",
    "\n",
    "以上我们完成了量子计算基测量的学习，接下来我们进入到另一种测量操作的学习：投影测量。\n",
    "\n",
    "## 投影测量\n",
    "\n",
    "投影测量(projective measuremen)由被观察系统状态空间上一个可观测量（observable）Hermite算子$M$来描述($M=M^{\\dagger}$)，该可观测量具有谱分解：\n",
    "\n",
    "$$\n",
    "M=\\Sigma_{m}mP_m\n",
    "$$\n",
    "\n",
    "这里的$P_m$是在$m$的特征值$m$对应特征空间上的投影，测量的可能结果对应于测量算子的特征值$m$。测量状态$|\\psi⟩$时，得到结果$m$的概率为\n",
    "\n",
    "$$\n",
    "p(m)=⟨\\psi|P_m|\\psi⟩\n",
    "$$\n",
    "\n",
    "测量后量子系统的状态立即为：\n",
    "\n",
    "$$\n",
    "\\frac{P_m|\\psi⟩}{\\sqrt{p(m)}}\n",
    "$$\n",
    "\n",
    "直观解释是，我们对状态$|\\psi⟩$使用$M$投影测量，是把$|\\psi⟩$往$M$的特征空间上投影，有$p_m$的概率投影到空间$V_{m}$中，此时测量结果为该空间对应的特征值$m$。\n",
    "\n",
    "投影测量一个重要的特征就是很容易计算投影测量的期望值$E(M)$。\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "    E(M) &=\\Sigma_i \\lambda_i p_i\\\\\n",
    "         &=\\Sigma_i \\lambda_i⟨\\psi|P_i|\\psi⟩\\\\\n",
    "         &=⟨\\psi|(\\Sigma_i\\lambda_i P_i)|\\psi⟩\\\\\n",
    "         &=⟨\\psi|M|\\psi⟩\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "投影测量可以视为一般测量的特殊情况，当测量算子除了满足完备性关系$\\Sigma_mM_m^\\dagger M_m=I$时，还满足$M_m$是正交投影算子的条件，即$M_m$是Hermite的，并且\n",
    "\n",
    "$$\n",
    "M_mM_{m'}=\\delta_{mm'}M_m\n",
    "$$\n",
    "\n",
    "有了这些附加限制，一般测量退化成投影测量。\n",
    "\n",
    "## Pauli测量\n",
    "\n",
    "最后我们学习Pauli测量，Pauli测量是投影测量中把可观测量$M$选取为泡利算子。以Pauli-Z测量为例，我们考虑Z算子：\n",
    "\n",
    "$$\n",
    "Z=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "$$\n",
    "\n",
    "可以看出，Z满足$Z=Z^\\dagger$，即Z是Hermite的。Z有两个特征值+1，-1，对应的特征向量分别为：|0⟩和|1⟩。因此Z的谱分解形式为：\n",
    "\n",
    "$$\n",
    "Z=\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\n",
    "    \\end{array}\n",
    "\\right)=1\\times|0⟩⟨0|+(-1)\\times|1⟩⟨1|\n",
    "$$\n",
    "\n",
    "使用Z做投影测量，如果测量结果为+1，我们可得出该量子比特的状态被投影到Z算子的+1特征子空间$V_{+1}$中，表明被测量态被投影成了|0⟩，相似地，如果测量结果为-1，可得出该量子比特被投影到-1特征子空间$V_{-1}$中，表明被测量态被投影成了|1⟩，这即为Pauli-Z测量。\n",
    "\n",
    "MindQuantum中为我们提供了基于给定可观测量H计算投影测量期望值的功能：\n",
    "\n",
    "`get_expectation(hamiltonian)`可以计算出模拟器当前量子态关于某个观察量的期望值：$E=⟨\\psi|H|\\psi⟩$。**该操作不会改变量子态**。\n",
    "\n",
    "例如，我们希望对处于$\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$态的系统上的q1比特上作用一个Pauli-Z测量，首先我们将模拟器置位："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.70710678+0.j 0.        +0.j 0.        +0.j 0.70710678+0.j]\n"
     ]
    }
   ],
   "source": [
    "sim = Simulator('projectq', 2)                        # 声明一个2比特的projectq模拟器\n",
    "sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2]))  # 设置模拟器状态\n",
    "print(sim.get_qs())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后我们构造出在q1上做Pauli-Z测量对应的哈密顿量hams:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mindquantum import Hamiltonian  # 引入哈密顿量定义模块\n",
    "from mindquantum.core.operators import QubitOperator  # 引入稀疏算子定义模块\n",
    "\n",
    "hams = Hamiltonian(QubitOperator('Z1'))  # 构建在q1上作Pauli-Z测量的哈密顿量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了深刻认识学习Pauli-Z测量操作，我们先手动计算出模拟器当前量子态在q1上做Pauli-Z测量的期望值，并推算出测量结果为+1，-1的概率：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "E&=⟨\\psi|H|\\psi⟩\\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "(Z \\otimes I) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\\\\\n",
    "    \\end{array}\n",
    "\\right) \\otimes\n",
    "\\left(\n",
    "\\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&1\\\\\n",
    "\\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0&0&0\\\\\n",
    "    0&1&0&0\\\\\n",
    "    0&0&-1&0\\\\\n",
    "    0&0&0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "0\\\\\n",
    "&=1\\times p(1)+(-1)\\times p(-1)\\\\\n",
    "&=1\\times p(1)+(-1)\\times (1-p(1))\\\\\n",
    "&=p(1)-1+p(-1)\\\\\n",
    "\\Longrightarrow&p(1)=p(-1)=0.5\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "这说明测量的理论期望值为0，测量出+1，-1的概率均为50%，我们使用MindQuantum提供的`get_expectation()`来验证结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0j"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.get_expectation(hams)  # 计算出模拟器当前量子态关于hams的期望值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，手动计算和使用`get_expectation(hamiltonian)`计算出的结果相同，符合预期。\n",
    "\n",
    "我们还可以对处于$\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$态的系统上的q0,q1比特上均作用Pauli-Z测量。类似地构造出在q0,q1上做Pauli-Z测量对应的哈密顿量hams2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "hams2 = Hamiltonian(QubitOperator('Z0') +\n",
    "                    QubitOperator('Z1'))  # 构建在q0,q1上作Pauli-Z测量的哈密顿量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们同样可以手动计算出模拟器当前量子态在q0,q1上做Pauli-Z测量的期望值：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "E&=⟨\\psi|H|\\psi⟩\\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "(Z \\otimes I) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right)\n",
    "+\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "(I \\otimes Z) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\\\\\n",
    "    \\end{array}\n",
    "\\right) \\otimes\n",
    "\\left(\n",
    "\\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&1\\\\\n",
    "\\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right)\n",
    "+\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&1\\\\\n",
    "    \\end{array}\n",
    "\\right) \\otimes\n",
    "\\left(\n",
    "\\begin{array}{l}\n",
    "    1&0\\\\\n",
    "    0&-1\\\\\n",
    "\\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0&0&0\\\\\n",
    "    0&1&0&0\\\\\n",
    "    0&0&-1&0\\\\\n",
    "    0&0&0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right)\n",
    "+\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}& 0& 0& \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    1&0&0&0\\\\\n",
    "    0&-1&0&0\\\\\n",
    "    0&0&1&0\\\\\n",
    "    0&0&0&-1\n",
    "    \\end{array}\n",
    "\\right)\n",
    "\\times\n",
    "\\left(\n",
    "    \\begin{array}{l}\n",
    "    \\frac{\\sqrt{2}}{2}\\\\\n",
    "    0\\\\\n",
    "    0\\\\\n",
    "    \\frac{\\sqrt{2}}{2}\n",
    "    \\end{array}\n",
    "\\right) \\\\&=\n",
    "0+0 \\\\\n",
    "&=0\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0j"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2]))  # 设置模拟器状态\n",
    "sim.get_expectation(hams2)                            # 计算出模拟器当前量子态关于hams2的期望值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "该操作不会改变量子态，我们查看当前量子态："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim.get_qs()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以发现，量子态依然是最初设定的$\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$\n",
    "\n",
    "我们学习认识了量子计算中重要的一个操作——测量，还使用MindQuantum测量量子线路验证我们的理论结果，并使用不同可视化工具展示出测量结果。\n",
    "\n",
    "想学习MindQuantum中量子线路的高阶操作，构建并训练量子经典混合神经网络，请查看`get_expectation_with_grad()`和`apply_hamitonian()`的文档。\n",
    "\n",
    "若想查询更多关于MindQuantum的API，请点击：[https://mindspore.cn/mindquantum/](https://mindspore.cn/mindquantum/)。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
